package com.fourth.controller.api;

import com.fourth.dao.UBbusinessDao;
import com.fourth.service.EmpInfoService;
import com.ljx.entity.BusinessInfo;
import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import com.ljx.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2023/2/28 10:55
 * @Version 1.0
 */
@Api(value = "/system/api", tags = {": TODO"})
@RestController
@RequestMapping("/system/api")
public class ApiEmpController {


	@Autowired
	private EmpInfoService empInfoService;

	/**
	 * 后端修改密码
	 * @param newPassword
	 * @param oldPassword
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataType = "string", name = "newPassword", value = "", required = true),
			@ApiImplicitParam(paramType = "path", dataType = "string", name = "oldPassword", value = "", required = true)
	})
	@ApiOperation(value = "后端修改密码", notes = "后端修改密码")
	@RequestMapping("/emp/updatePassword/{newPassword}/{oldPassword}")
	public Result updatePassword(@PathVariable("newPassword") String newPassword,@PathVariable("oldPassword") String oldPassword){

		return empInfoService.updatePassword(newPassword,oldPassword);
	}

	@Autowired
	private StringRedisTemplate redisTemplate;

	/**
	 * 退出登录将redis 中 token清空
	 * @return
	 */
	@ApiOperation(value = "退出登录将redis 中 token清空", notes = "退出登录将redis 中 token清空", httpMethod = "GET")
	@GetMapping("/sso/loginOut")
	public Result<String> logout(){
		String token = WebUtils.getRequest().getHeader("token");
		redisTemplate.delete(token);
		return new Result<>(2000,"退出成功");
	}

	@Autowired(required = false)
	private UBbusinessDao uBbusinessDao;
	@RequestMapping("/loginName")
	public Result loginName(){
		HttpServletRequest request = WebUtils.getRequest();
		String token = request.getHeader("token");
		Map<String, Object> map = JwtUtil.getTokenChaim(token);
		String username = (String) map.get("username");
		return new Result(2000,"查询成功name",username);
	}
}
